﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zatify.WebUI.Admin.Models;
using System.Text;
using Zatify.WebUI.Models;

namespace Zatify.WebUI.Admin.HtmlHelpers
{
    public static class PagingHelpers
    {
        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl,
            string firstPageImgSrc, string lastPageImgSrc,
            string prevPageImgSrc, string nextPageImgSrc)
        {
            StringBuilder result = new StringBuilder();

            var tableTag = new TagBuilder("table");
            var tagTr = new TagBuilder("tr");

            result.Append(GetFirstPageButton(pageUrl(1), firstPageImgSrc));

            result.Append(GetPreviousPageButton(pageUrl(pagingInfo.CurrentPage > 1 ? pagingInfo.CurrentPage - 1 : 1), prevPageImgSrc));

            for (int i = 1; i <= pagingInfo.TotalPages; i++)
            {
                TagBuilder tagTd = new TagBuilder("td");
                tagTd.AddCssClass("pagination_no");

                TagBuilder tagA = new TagBuilder("a");
                tagA.MergeAttribute("href", pageUrl(i));
                tagA.InnerHtml = i.ToString();

                if (i == pagingInfo.CurrentPage)
                    tagA.AddCssClass("selected");

                tagTd.InnerHtml = tagA.ToString();
                result.Append(tagTd.ToString());
            }

            result.Append(GetNextPageButton(pageUrl(pagingInfo.CurrentPage < pagingInfo.TotalPages ? pagingInfo.CurrentPage + 1 : pagingInfo.TotalPages), nextPageImgSrc));

            result.Append(GetLastPageButton(pageUrl(pagingInfo.TotalPages), lastPageImgSrc));

            tagTr.InnerHtml = result.ToString();

            tableTag.InnerHtml = tagTr.ToString();

            return MvcHtmlString.Create(result.ToString());
        }

        private static string GetFirstPageButton(string pageUrl, string imgSrc)
        {
            var tagTdFirstPage = new TagBuilder("td");
            tagTdFirstPage.MergeAttribute("class", "btn_first");
            tagTdFirstPage.MergeAttribute("width", "10px");

            var tagAFirstPage = new TagBuilder("a");
            tagAFirstPage.MergeAttribute("href", pageUrl);

            var tagImgFirstPage = new TagBuilder("img");
            tagImgFirstPage.MergeAttribute("src", imgSrc);
            tagImgFirstPage.MergeAttribute("title", "First");

            tagAFirstPage.InnerHtml = tagImgFirstPage.ToString();
            tagTdFirstPage.InnerHtml = tagAFirstPage.ToString();

            return tagTdFirstPage.ToString();
        }

        private static string GetPreviousPageButton(string pageUrl, string imgSrc)
        {
            var tagTdFirstPage = new TagBuilder("td");
            tagTdFirstPage.MergeAttribute("class", "btn_previous");
            tagTdFirstPage.MergeAttribute("width", "10px");

            var tagAFirstPage = new TagBuilder("a");
            tagAFirstPage.MergeAttribute("href", pageUrl);

            var tagImgFirstPage = new TagBuilder("img");
            tagImgFirstPage.MergeAttribute("src", imgSrc);
            tagImgFirstPage.MergeAttribute("title", "First");

            tagAFirstPage.InnerHtml = tagImgFirstPage.ToString();
            tagTdFirstPage.InnerHtml = tagAFirstPage.ToString();

            return tagTdFirstPage.ToString();
        }

        private static string GetNextPageButton(string pageUrl, string imgSrc)
        {
            var tagTdFirstPage = new TagBuilder("td");
            tagTdFirstPage.MergeAttribute("class", "btn_next");
            tagTdFirstPage.MergeAttribute("width", "10px");

            var tagAFirstPage = new TagBuilder("a");
            tagAFirstPage.MergeAttribute("href", pageUrl);

            var tagImgFirstPage = new TagBuilder("img");
            tagImgFirstPage.MergeAttribute("src", imgSrc);
            tagImgFirstPage.MergeAttribute("title", "First");

            tagAFirstPage.InnerHtml = tagImgFirstPage.ToString();
            tagTdFirstPage.InnerHtml = tagAFirstPage.ToString();

            return tagTdFirstPage.ToString();
        }

        private static string GetLastPageButton(string pageUrl, string imgSrc)
        {
            var tagTdFirstPage = new TagBuilder("td");
            tagTdFirstPage.MergeAttribute("class", "btn_last");
            tagTdFirstPage.MergeAttribute("width", "10px");

            var tagAFirstPage = new TagBuilder("a");
            tagAFirstPage.MergeAttribute("href", pageUrl);

            var tagImgFirstPage = new TagBuilder("img");
            tagImgFirstPage.MergeAttribute("src", imgSrc);
            tagImgFirstPage.MergeAttribute("title", "First");

            tagAFirstPage.InnerHtml = tagImgFirstPage.ToString();
            tagTdFirstPage.InnerHtml = tagAFirstPage.ToString();

            return tagTdFirstPage.ToString();
        }
    }
}